package com.sphd.emanu.activity.ordersdetail;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.adapter.OrdersDetailAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.order.Order;
import com.sphd.emanu.pojo.order.OrderItems;
import com.sphd.emanu.utils.PermissionConfig;
import com.sphd.emanu.utils.TypeEnum;
import com.wzutils.BigDisMailUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 订单详情
 *
 * @Author Hu
 * @Time 2017/8/5 -- 10:11.
 * @Email 87683202@qq.com
 */
public class OrdersdetailActivity extends MVPBaseActivity<OrdersdetailContract.View, OrdersdetailPresenter> implements OrdersdetailContract.View {
    Intent intent;
    Bundle bundle;
    Order order;
    OrdersDetailAdapter ordersDetailAdapter;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.user_phone)
    TextView userPhone;
    @BindView(R.id.order_state)
    TextView orderState;
    @BindView(R.id.item_btn_rec)
    Button itemBtnRec;
    @BindView(R.id.item_btn_cancle)
    Button itemBtnCancle;
    @BindView(R.id.item_send)
    Button itemSend;
    @BindView(R.id.item_pay_bill)
    Button itemPayBill;
    @BindView(R.id.item_print_p)
    Button itemPrintP;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    View footView;
    TextView all_money, coupon_pay, real_pay, card_pay, money_pay, points_pay, order_adr, order_name, order_phone;
    List<Moudle> moudles;
    boolean isCanDo = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ordersdetail);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
    }

    private void initView() {
        setTitleBar(toolbar, "订单详情");
        intent = getIntent();
        bundle = intent.getExtras();
        order = (Order) bundle.getSerializable(Constant.ORDER_ENTITY);
        moudles = (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo = isCanDo(moudles, PermissionConfig.MENU_RECEIVE_2);
        initAdapter();
        initFootView();
        addData();
    }

    private void initFootView() {
        footView = View.inflate(context, R.layout.item_footview_orders_detail, null);
        all_money = (TextView) footView.findViewById(R.id.all_money);
        coupon_pay = (TextView) footView.findViewById(R.id.coupon_pay);
        real_pay = (TextView) footView.findViewById(R.id.real_pay);
        card_pay = (TextView) footView.findViewById(R.id.card_pay);
        money_pay = (TextView) footView.findViewById(R.id.money_pay);
        points_pay = (TextView) footView.findViewById(R.id.points_pay);
        order_adr = (TextView) footView.findViewById(R.id.order_adr);
        order_name = (TextView) footView.findViewById(R.id.order_name);
        order_phone = (TextView) footView.findViewById(R.id.order_phone);
        ordersDetailAdapter.addFooterView(footView);
    }

    private void initAdapter() {
        setRecy(myRecycler);
        ordersDetailAdapter = new OrdersDetailAdapter(R.layout.item_order_detail, new ArrayList<OrderItems>(), true);
        myRecycler.setAdapter(ordersDetailAdapter);
    }

    private void addData() {
        showProgressDialog();
        mPresenter.selectOrderItems(order.getId());
    }

    @Override
    public void setBean(Order order) {
        dissProgressDialog();
        userName.setText(TextUtils.isEmpty(order.getMemName()) ? "无姓名" : order.getMemName());
        userPhone.setText(order.getMemPhone());
        orderState.setText(TypeEnum.getByValue(Integer.parseInt(order.getStatus())));
        all_money.setText("￥" + order.getAmount());
        coupon_pay.setText("￥" + order.getCoupon());
        real_pay.setText("￥" + BigDisMailUtils.turn(order.getDeposit(), Constant.POINT).add(order.getAmountPaid()));
        card_pay.setText("￥" + BigDisMailUtils.turnTrim(order.getDeposit(), Constant.POINT));
        money_pay.setText("￥" + order.getAmountPaid());
        points_pay.setText("￥" + (order.getPoints() == null ? 0 : order.getPoints()));
        order_adr.setText(order.getAddress());
        order_name.setText(order.getConsignee());
        order_phone.setText(order.getPhone());
        ordersDetailAdapter.setNewData(order.getListOrderItems());
        /**
         status订单状态,0-订单取消,1-订单提交,2-订单接受,3-订单拒绝,4-订单派送,5-订单完成,9-过期订单
         原型上的状态顺序为12450
         原型上的取消按钮变为拒绝,
         修改订单状态时,要变成啥状态就传对应的状态值,比如,要接受,就传2,要取消,就传0
         */
        if (order.getStatus().equals("1")) {
            itemBtnRec.setVisibility(View.VISIBLE);
            itemBtnCancle.setVisibility(View.VISIBLE);
        } else if (order.getStatus().equals("2")) {
            itemSend.setVisibility(View.VISIBLE);
        } else if (order.getStatus().equals("4")) {
            itemPayBill.setVisibility(View.VISIBLE);
        } else if (order.getStatus().equals("5")) {
            itemPrintP.setVisibility(View.VISIBLE);
        }


    }

    @Override
    public void recRes(int res, int i) {
        dissProgressDialog();
        if (res == 1) {
            toast(getString(R.string.up_success));
            if (i == 2) {
                intent.putExtra(Constant.FLAG, 1);
            } else if (i == 0) {
                intent.putExtra(Constant.FLAG, 4);
            } else if (i == 4) {
                intent.putExtra(Constant.FLAG, 2);
            } else if (i == 5) {
                intent.putExtra(Constant.FLAG, 3);
            }
            setResult(Constant.REQUEST_CODE, intent);
            finish();
        } else {
            toast(getString(R.string.up_failure));
        }
    }

    @OnClick({R.id.item_btn_rec, R.id.item_btn_cancle, R.id.item_send, R.id.item_pay_bill, R.id.item_print_p})
    public void onViewClicked(View view) {
        if (isCanDo) {
            switch (view.getId()) {

                case R.id.item_btn_rec:
                    //订单接收
                    changeStatus(order, 2);
                    break;
                case R.id.item_btn_cancle:
                    //订单拒绝即为取消
                    changeStatus(order, 0);
                    break;
                case R.id.item_send:
                    //派送
                    changeStatus(order, 4);
                    break;
                case R.id.item_pay_bill:
                    //结账
                    changeStatus(order, 5);
                    break;
                case R.id.item_print_p:
                    //打印小票
                    toast("暂未开通");
                    break;
            }
        } else {
            toast(getString(R.string.no_perssion));
        }

    }

    private void changeStatus(Order order, int state) {
        showProgressDialog();
        mPresenter.updateOrderStatus(order.getId(), state);
    }
}
